#include "enemy.h"

#include "affiliate/affiliateBar.h"
#include "affiliate/sprite_anim.h"
#include "core/scene.h"
#include "raw/stats.h"

void Enemy::aim_target(Player *target) {
    if (target_ == nullptr) // 如果没有目标
    {
        return; // 返回
    }
    auto direction = target->getPosition() - this->getPosition(); // 计算方向
    direction = glm::normalize(direction); // 归一化方向
    velocity_ = direction * max_speed_; // 设置速度
}

void Enemy::checkState() {
    State new_state;
    if (stats_->getHealth() <= 0) {
        new_state = State::DIE;
    } else if (stats_->isInvicible()) {
        new_state = State::HURT;
    } else {
        new_state = State::NORMAL;
    }
    if (new_state != current_state_) {
        changeState(new_state);
    }
}

void Enemy::changeState(State new_state) {
    if (new_state == current_state_) {
        return;
    }
    current_anim_->setActive(false);
    switch (new_state) {
        case State::NORMAL:
            current_anim_ = anim_normal_;
            current_anim_->setActive(true);
            break;
        case State::HURT:
            current_anim_ = anim_hurt_;
            current_anim_->setActive(true);
            break;
        case State::DIE:
            current_anim_ = anim_die_;
            current_anim_->setActive(true);
            game_.addScore(score_);
            break;
    }
    current_state_ = new_state;
}

void Enemy::remove() {
    if (anim_die_->getFinished()) {
        need_remove_ = true;
    }
}

void Enemy::attack() {
    if (!collider_ || !target_ || target_->getCollider() == nullptr) {
        return;
    }
    if (collider_->isColliding(target_->getCollider())) {
        if (stats_ && target_->getStats()) {
            target_->takeDamage(stats_->getDamage());
        }
    }
}

void Enemy::update(float dt) {
    Actor::update(dt); // 更新父类
    if (target_->getActive()) {
        aim_target(target_); // 瞄准目标
        move(dt); // 移动
        attack(); //攻击
    }
    checkState();
    remove();
}

Enemy *Enemy::addEnemyChild(Object *parent, glm::vec2 pos, Player *target) {
    auto enemy = new Enemy();
    enemy->init();
    enemy->setPosition(pos);
    enemy->setTarget(target);
    if (parent) {
        parent->addChild(enemy);
    }
    return enemy;
}

void Enemy::init() {
    Actor::init(); // 初始化父类
    setType(ObjectType::ENEMY);
    anim_normal_ = SpriteAnim::addSpriteAnimChild(this, "resources/sprite/ghost-Sheet.png", 2.0f); // 添加精灵动画
    anim_die_ = SpriteAnim::addSpriteAnimChild(this, "resources/sprite/ghostDead-Sheet.png", 2.0f); // 添加精灵动画
    anim_hurt_ = SpriteAnim::addSpriteAnimChild(this, "resources/sprite/ghostHurt-Sheet.png", 2.0f); // 添加精灵动画
    anim_hurt_->setActive(false);
    anim_die_->setActive(false);
    anim_die_->setLoop(false);
    current_anim_ = anim_normal_;
    collider_ = Collider::addColliderChild(this, current_anim_->getSize());
    stats_ = Stats::addStatsChild(this);
    auto size = anim_normal_->getSize();
    healthBar_ = AffiliateBar::addAffiliateBarChild(this, glm::vec2(size.x, 10), glm::vec2(0), Anchor::BOTTOM_CENTER);
    healthBar_->setOffset(healthBar_->getOffset() + glm::vec2(0, size.y / 2));
}
